{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Updating a Historical Marquee Portfolio\n",
    "\n",
    "If you already have a portfolio in Marquee, the GS Quant SDK provides a simple and intuitive workflow to update positions and rerun reports.\n",
    "\n",
    "You will need to be an admin on the portfolio you would like to update. If you are not an admin, please ask a portfolio admin to [edit the portfolio's entitlements](../examples/marquee/01_edit_portfolio_entitlements.ipynb) to include you."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 1: Authenticate and Initialize Your Session\n",
    "\n",
    "First you will import the necessary modules and add your client id and client secret."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import datetime as dt\n",
    "\n",
    "from gs_quant.common import PositionSet\n",
    "from gs_quant.markets.portfolio_manager import PortfolioManager\n",
    "from gs_quant.session import GsSession, Environment\n",
    "\n",
    "client = None\n",
    "secret = None\n",
    "scopes = None\n",
    "\n",
    "## External users must fill in their client ID and secret below and comment out the line below\n",
    "\n",
    "#client = 'ENTER CLIENT ID'\n",
    "#secret = 'ENTER CLIENT SECRET'\n",
    "#scopes = ('read_product_data read_financial_data modify_financial_data run_analytics',)\n",
    "\n",
    "GsSession.use(\n",
    "    Environment.PROD,\n",
    "    client_id=client,\n",
    "    client_secret=secret,\n",
    "    scopes=scopes\n",
    ")\n",
    "\n",
    "print('GS Session initialized.')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 2: Define Your Portfolio ID and the Positions You Would Like to Upload\n",
    "\n",
    "Portfolio positions in Marquee are stored on a holding basis, when means you only upload positions for days where you are rebalancing your portfolio. Take the following set of positions:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "portfolio_id = 'ENTER PORTFOLIO ID'\n",
    "portfolio_position_sets = [\n",
    "    PositionSet.from_dicts(\n",
    "        date=dt.date(day=3, month=5, year=2021),\n",
    "        positions=[\n",
    "            {\n",
    "                'identifier': 'AAPL UW',\n",
    "                'quantity': 25\n",
    "            }, {\n",
    "                'identifier': 'GS UN',\n",
    "                'quantity': 50\n",
    "            }]\n",
    "    ),\n",
    "    PositionSet.from_dicts(\n",
    "        date=dt.date(day=1, month=7, year=2021),\n",
    "        positions=[\n",
    "            {\n",
    "                'identifier': 'AAPL UW',\n",
    "                'quantity': 26\n",
    "            }, {\n",
    "                'identifier': 'GS UN',\n",
    "                'quantity': 51\n",
    "            }]\n",
    "    )\n",
    "]\n",
    "\n",
    "for pos_set in portfolio_position_sets:\n",
    "    pos_set.resolve()\n",
    "    if len(pos_set.unresolved_positions) > 0:\n",
    "        print(f'The following positions on {pos_set.date} could not be resolved: {[p.identifier for p in pos_set.unresolved_positions]}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Have your positions as a dataframe?\n",
    "\n",
    "If you have a day's positions in a dataframe with columns `identifer` (string values), `quantity` (float values),\n",
    "and optionally `tags` (dictionary values), you can turn them into a `PositionSet` object by using the\n",
    "`PositionSet.from_frame()` function:\n",
    "\n",
    "`position_set = PositionSet.from_frame(positions_df, datetime_date)`\n",
    "\n",
    "## Step 3: Post Positions to the Marquee Portfolio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "pm = PortfolioManager(portfolio_id)\n",
    "pm.update_positions(portfolio_position_sets)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 4: Reschedule All Portfolio Reports\n",
    "\n",
    "Now that the portfolio has new positions, it's time to rerun all reports associated with the portfolio so your performance, risk, and other analytics reflect these new positions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "pm.schedule_reports()\n",
    "\n",
    "print('All portfolio reports scheduled.')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Other questions? Reach out to the [Portfolio Analytics team](mailto:gs-marquee-analytics-support@gs.com)!*"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}